package com.bear.atlassianmcp.jira.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * Jira Issue数据模型
 * 对应Jira REST API返回的Issue对象
 */
@JsonIgnoreProperties(ignoreUnknown = true)
public class JiraIssue {

    private String id;
    private String key;
    private String self;
    private Map<String, Object> fields;

    // 常用字段的便捷访问方法
    public String getSummary() {
        return getFieldAsString("summary");
    }

    public String getDescription() {
        return getFieldAsString("description");
    }

    public String getStatus() {
        Map<String, Object> status = getFieldAsMap("status");
        return status != null ? (String) status.get("name") : null;
    }

    public String getIssueType() {
        Map<String, Object> issueType = getFieldAsMap("issuetype");
        return issueType != null ? (String) issueType.get("name") : null;
    }

    public String getPriority() {
        Map<String, Object> priority = getFieldAsMap("priority");
        return priority != null ? (String) priority.get("name") : null;
    }

    public String getAssignee() {
        Map<String, Object> assignee = getFieldAsMap("assignee");
        return assignee != null ? (String) assignee.get("displayName") : null;
    }

    public String getReporter() {
        Map<String, Object> reporter = getFieldAsMap("reporter");
        return reporter != null ? (String) reporter.get("displayName") : null;
    }

    public String getProject() {
        Map<String, Object> project = getFieldAsMap("project");
        return project != null ? (String) project.get("key") : null;
    }

    public String getCreated() {
        return getFieldAsString("created");
    }

    public String getUpdated() {
        return getFieldAsString("updated");
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getComments() {
        Map<String, Object> comment = getFieldAsMap("comment");
        if (comment != null && comment.containsKey("comments")) {
            return (List<Map<String, Object>>) comment.get("comments");
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public List<Map<String, Object>> getAttachments() {
        if (fields != null && fields.containsKey("attachment")) {
            Object value = fields.get("attachment");
            return value instanceof List ? (List<Map<String, Object>>) value : null;
        }
        return null;
    }

    public int getAttachmentCount() {
        List<Map<String, Object>> attachments = getAttachments();
        return attachments != null ? attachments.size() : 0;
    }

    public boolean hasAttachments() {
        return getAttachmentCount() > 0;
    }

    // 辅助方法
    private String getFieldAsString(String fieldName) {
        if (fields != null && fields.containsKey(fieldName)) {
            Object value = fields.get(fieldName);
            return value != null ? value.toString() : null;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    private Map<String, Object> getFieldAsMap(String fieldName) {
        if (fields != null && fields.containsKey(fieldName)) {
            Object value = fields.get(fieldName);
            return value instanceof Map ? (Map<String, Object>) value : null;
        }
        return null;
    }

    /**
     * 格式化文件大小
     */
    private String formatFileSize(Long size) {
        if (size == null) {
            return "未知大小";
        }

        if (size < 1024) {
            return size + " B";
        } else if (size < 1024 * 1024) {
            return String.format("%.1f KB", size / 1024.0);
        } else if (size < 1024 * 1024 * 1024) {
            return String.format("%.1f MB", size / (1024.0 * 1024.0));
        } else {
            return String.format("%.1f GB", size / (1024.0 * 1024.0 * 1024.0));
        }
    }

    /**
     * 格式化为AI友好的文本描述
     */
    public String toFormattedString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Issue: ").append(key).append("\n");
        sb.append("Summary: ").append(getSummary()).append("\n");
        sb.append("Status: ").append(getStatus()).append("\n");
        sb.append("Type: ").append(getIssueType()).append("\n");
        sb.append("Priority: ").append(getPriority()).append("\n");
        sb.append("Assignee: ").append(getAssignee()).append("\n");
        sb.append("Reporter: ").append(getReporter()).append("\n");
        sb.append("Project: ").append(getProject()).append("\n");
        sb.append("Created: ").append(getCreated()).append("\n");
        sb.append("Updated: ").append(getUpdated()).append("\n");
        
        String description = getDescription();
        if (description != null && !description.trim().isEmpty()) {
            sb.append("Description: ").append(description).append("\n");
        }

        // 添加附件信息
        if (hasAttachments()) {
            sb.append("Attachments: ").append(getAttachmentCount()).append(" file(s)").append("\n");

            // 显示前3个附件的简要信息
            List<Map<String, Object>> attachments = getAttachments();
            int displayCount = Math.min(3, attachments.size());
            for (int i = 0; i < displayCount; i++) {
                Map<String, Object> attachment = attachments.get(i);
                String filename = (String) attachment.get("filename");
                Long size = attachment.get("size") instanceof Number ?
                           ((Number) attachment.get("size")).longValue() : null;
                String sizeStr = formatFileSize(size);
                sb.append("  - ").append(filename).append(" (").append(sizeStr).append(")").append("\n");
            }

            if (attachments.size() > 3) {
                sb.append("  ... 还有 ").append(attachments.size() - 3).append(" 个附件\n");
            }
        }

        return sb.toString();
    }

    // Getters and Setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getSelf() {
        return self;
    }

    public void setSelf(String self) {
        this.self = self;
    }

    public Map<String, Object> getFields() {
        return fields;
    }

    public void setFields(Map<String, Object> fields) {
        this.fields = fields;
    }
}
